import {
  AuthModel
} from '../../models/hdd/authData.js'
let authModel = new AuthModel()
import {
  PersonModel
} from '../../models/hdd/personal.js'
let personModel = new PersonModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    businessLin: '',
    roadLin: '',
    userPhoto: '',
    businessLinPicId: '',
    roadLinPicId: '',
    userPhotoPicId: '',
    FCompanyId: '',
    FId: '',
  },

  selectBusiLin(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          businessLin: res.tempFilePaths[0]
        })
      }
    })
  },

  selectRoadLin(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          roadLin: res.tempFilePaths[0]
        })
      }
    })
  },

  selectUserPhoto(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          userPhoto: res.tempFilePaths[0]
        })
      }
    })
  },

  checkData() {
    if (this.data.businessLin.length == 0) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请上传工商营业执照'
      })
      return
    }
    wx.showLoading({
      mask: true
    })
    this.uploadBusinessLin()
  },

  uploadBusinessLin() {
    var uuid = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/;
    if (uuid.test(this.data.businessLin)) {
      this.setData({
        businessLinPicId: this.data.businessLin
      })
      this.uploadRoadLin()
      return
    }
    let that = this
    wx.uploadFile({
      url: 'https://www.myhr100.com:4200/api/Ecp.Picture.upload.jup',
      filePath: this.data.businessLin,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
        'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
      },
      formData: {
        'user': 'test'
      },
      success: function(res) {
        that.setData({
          businessLinPicId: JSON.parse(res.data).data.pictureId
        })
      },
      fail: function(e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function() {
        that.uploadRoadLin()
      }
    })
  },

  uploadRoadLin() {
    var uuid = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/;
    if (uuid.test(this.data.roadLin)) {
      this.setData({
        roadLinPicId: this.data.roadLin
      })
      this.uploadUserPhoto()
      return
    }
    let that = this
    if (this.data.userPhoto.length == 0) {
      that.uploadUserPhoto()
      return
    } else {
      wx.uploadFile({
        url: 'https://www.myhr100.com:4200/api/Ecp.Picture.upload.jup',
        filePath: that.data.roadLin,
        name: 'file',
        header: {
          "Content-Type": "multipart/form-data",
          'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
        },
        formData: {
          'user': 'test'
        },
        success: function(res) {
          that.setData({
            roadLinPicId: JSON.parse(res.data).data.pictureId
          })
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
        },
        fail: function(e) {
          console.log(e);
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function() {
          that.uploadUserPhoto()
        }
      })
    }
  },

  uploadUserPhoto() {
    var uuid = /[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}/;
    if (uuid.test(this.data.userPhoto)) {
      this.setData({
        userPhotoPicId: this.data.userPhoto
      })
      this.saveData()
      return
    }
    let that = this
    if (this.data.userPhoto.length == 0) {
      that.saveData()
      return
    } else {
      wx.uploadFile({
        url: 'https://www.myhr100.com:4200/api/Ecp.Picture.upload.jup',
        filePath: that.data.userPhoto,
        name: 'file',
        header: {
          "Content-Type": "multipart/form-data",
          'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
        },
        formData: {
          'user': 'test'
        },
        success: function(res) {
          that.setData({
            userPhotoPicId: JSON.parse(res.data).data.pictureId
          })
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
        },
        fail: function(e) {
          console.log(e);
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function() {
          that.saveData()
        }
      })
    }
  },

  saveData() {
    let param
    if(this.data.FId) {
      param = {
        FBusinessLisence: this.data.businessLinPicId,
        FWayLicence: this.data.roadLinPicId,
        FCompamyPhoto: this.data.userPhotoPicId,
        FCompanyId: this.data.FCompanyId,
        FId: this.data.FId
      }
    } else {
      param = {
        FBusinessLisence: this.data.businessLinPicId,
        FWayLicence: this.data.roadLinPicId,
        FCompamyPhoto: this.data.userPhotoPicId,
        FCompanyId: this.data.FCompanyId
      }
    }

    const saveDataToServer = authModel.saveData({
      args: {
        data: [param]
      }
    })
    saveDataToServer.then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.success) {
        wx.showModal({
          title: '提示',
          content: '保存成功',
          showCancel: false
        })
        wx.reLaunch({
          url: '/pages/hdd-home/hdd-home',
        })
      }
    })
  },

  priSeriesSelector: function(e) {
    console.log(e.detail)
    this.setData({
      priSeriesIndex: e.detail.value
    })
  },
  priYearsSelector: function(e) {
    this.setData({
      priYearsIndex: e.detail.value
    })
  },
  secSeriesSelector: function(e) {
    this.setData({
      secSeriesIndex: e.detail.value
    })
  },
  secYearsSelector: function(e) {
    this.setData({
      secYearsIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '平台认证',
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF7136',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    // wx.showLoading({
    //   mask: true
    // })
    if (options.companyId) {
      this.setData({
        FCompanyId: options.companyId
      })
    }
    this.requestCompanyInfo()
  },

  requestCompanyInfo() {
    personModel.getPersonalData({
      FId: this.data.FCompanyId
    }).then(res => {
      if (res.data.data.data.company.platformAuthen) {
        this.setData({
          businessLin: res.data.data.data.company.platformAuthen.FBusinessLisence ? res.data.data.data.company.platformAuthen.FBusinessLisence : '',
          roadLin: res.data.data.data.company.platformAuthen.FWayLicence ? res.data.data.data.company.platformAuthen.FWayLicence : '',
          userPhoto: res.data.data.data.company.platformAuthen.FCompamyPhoto ? res.data.data.data.company.platformAuthen.FCompamyPhoto : '',
          FId: res.data.data.data.company.platformAuthen.FId ? res.data.data.data.company.platformAuthen.FId : '',
        })
      }
    })
  },

  initData() {
    console.log(this.data.priSeries)
    console.log(this.data.priYears)
    var priSeriesIndex, secSeriesIndex, priYearIndex, secYearIndex
    for (var i = 0; i < this.data.priSeries.length; i++) {
      if (this.data.priSeries[i].FId === this.data.authenticationInfo.FPrimarySeries) {
        priSeriesIndex = i
      }
      if (this.data.priSeries[i].FId === this.data.authenticationInfo.FSecondarySeries) {
        secSeriesIndex = i
      }
    }
    for (var i = 0; i < this.data.priYears.length; i++) {
      if (this.data.priYears[i].value === this.data.authenticationInfo.FPrimaryYear) {
        priYearIndex = i
      }
      if (this.data.priYears[i].value === this.data.authenticationInfo.FSecondaryYear) {
        secYearIndex = i
      }
    }
    this.setData({
      priSeriesIndex: priSeriesIndex ? priSeriesIndex : 0,
      priYearsIndex: priYearIndex ? priYearIndex : 0,
      secSeriesIndex: secSeriesIndex ? secSeriesIndex : 0,
      secYearsIndex: secYearIndex ? secYearIndex : 0,
      gatheringCode: this.data.authenticationInfo.FGatheringCode ? this.data.authenticationInfo.FGatheringCode : '',
      quaCertificate: this.data.authenticationInfo.FQualificationCertificate ? this.data.authenticationInfo.FQualificationCertificate.split('|') : [],
      idCertificate: this.data.authenticationInfo.FIdentityCardImg ? this.data.authenticationInfo.FIdentityCardImg.split('|') : [],
      awardCertificate: this.data.authenticationInfo.FAwardInformation ? this.data.authenticationInfo.FAwardInformation.split('|') : [],
      otherDes: this.data.authenticationInfo.FRemark ? this.data.authenticationInfo.FRemark : '',
    })
  },

  otherDesInput(e) {
    this.setData({
      otherDes: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})